package com.dg.river.core.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.dg.river.R;
import com.dg.river.core.http.ApiManager;
import com.dg.river.core.util.StartAtyUtils;
import com.dg.river.module.web.WebLinkActivity;

/**
 * Created by hy on 2023/04/11
 * Description : 隐私政策/用户协议弹窗
 */
public class PrivateUserDialog extends Dialog {

    public PrivateUserDialog(Context context) {
        super(context);
    }

    public PrivateUserDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context mContext;
        private boolean cancel;
        private OnDialogClickListener listener;
        private PrivateUserDialog dialog;

        public Builder(Context mContext) {
            this.mContext = mContext;
        }

        /**
         * 设置是否能够取消
         *
         * @param bool
         * @return
         */
        public Builder setCancel(boolean bool) {
            this.cancel = bool;
            return this;
        }

        public interface OnDialogClickListener {

            void onAgree(PrivateUserDialog dialog);

            void onQuite(PrivateUserDialog dialog);
        }

        /**
         * 设置监听器
         *
         * @param listener
         * @return
         */
        public Builder setDialogClickListener(OnDialogClickListener listener) {
            this.listener = listener;
            return this;
        }

        /**
         * 创建
         *
         * @return
         */
        public PrivateUserDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dialog = new PrivateUserDialog(mContext, R.style.dialog_normal);
            View layout = inflater.inflate(R.layout.dialog_private_user, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);

            TextView tvAgree = layout.findViewById(R.id.tvAgree);
            TextView tvQuit = layout.findViewById(R.id.tvQuit);
            TextView tvMessage = layout.findViewById(R.id.tvMessage);

            String message = "感谢您使用掌上小清河，为保护您的个人信息和隐私安全，在您使用掌上小清河之前，请仔细阅读《隐私政策》和《用户协议》，我们将按照您同意的各项条款使用您的个人信息，以便为您提供更好服务。";
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(message);
            spannableStringBuilder.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    WebLinkActivity.start(mContext,ApiManager.Web.PRIVATE_URL, "隐私政策");
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(ContextCompat.getColor(mContext, R.color.blue_0070f9));
                    ds.setUnderlineText(false);//设置下划线
                }
            }, 43, 49, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableStringBuilder.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    WebLinkActivity.start(mContext,ApiManager.Web.USER_URL, "用户协议");
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(ContextCompat.getColor(mContext, R.color.blue_0070f9));
                    ds.setUnderlineText(false);//设置下划线
                }
            }, 50, 56, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvMessage.setText(spannableStringBuilder);
            tvMessage.setHighlightColor(ContextCompat.getColor(mContext, R.color.white));
            tvMessage.setMovementMethod(LinkMovementMethod.getInstance());

            tvAgree.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onAgree(dialog);
                }
            });
            tvQuit.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onQuite(dialog);
                }
            });
            dialog.setContentView(layout);
            if (!cancel) {
                dialog.setCancelable(false);
            } else {
                dialog.setCancelable(true);
            }
            return dialog;
        }
    }
}
